package com.gfm.story.controller;

import com.gfm.story.entity.base.PageBean;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.dict.Dictionary;
import com.gfm.story.service.dict.DictionaryService;
import com.gfm.story.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Description: 数据字典
 * @Author: ly
 * @CreateDate: 2019/3/7 0007 下午 4:26
 * @UpdateUser: yc
 * @UpdateDate: 2019/3/7 0007 下午 4:26
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(tags = "系统管理-数据字典接口")
@RestController
@RequestMapping(value = "/dict", method = RequestMethod.POST)
public class DictionaryController {

    @Resource
    private DictionaryService sysDictionaryService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    /**
     * 获取所有数据字典列表
     *
     * @param request
     * @param pageBean
     * @return 列表数据带分页
     * @throws Exception
     */
    @RequestMapping("/getSysDictionaryPage.do")
    @ApiOperation(value = "所有按钮数据查询列表", httpMethod = "POST", response = ResultBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictName", value = "名称", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "start", value = "起始页码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "rows", value = "每页数量", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "dictCode", value = "编码", dataType = "String", paramType = "query", required = false),

    })
    public ResultBean getSysDictionaryPage(HttpServletRequest request, PageBean pageBean) throws Exception {
        Map<String, Object> map = ParamUtil.getParameterMap(request);
        String dictName = request.getParameter("dictName");

        if (null != dictName) {
            map.put("dictName", dictName);
        }
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getRows());
        ResultBean resultBean = this.sysDictionaryService.getSysDictionaryPage(map);
        return resultBean;
    }


    /**
     * 新增或者修改数据字典信息
     *
     * @param sysDictionary
     * @return
     */
    @RequestMapping("/addOrEdit.do")
    @ApiOperation(value = "新增或者修改数据字典信息", httpMethod = "POST", response = ResultBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictName", value = "名称", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "dictCode", value = "编码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "disc", value = "描述", dataType = "String", paramType = "query", required = false)
    })
    public ResultBean addOrEdit(@Validated Dictionary sysDictionary) {
        return this.sysDictionaryService.addOrEdit(sysDictionary);
    }

    /**
     * 删除数据字典
     *
     * @param id
     * @return
     */

    @RequestMapping("/remove.do")
    @ApiOperation(value = "删除数据字典信息", httpMethod = "POST", response = ResultBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据字典ID", dataType = "String", paramType = "query", required = true)
    })
    public ResultBean remove(Long id) {
        return this.sysDictionaryService.remove(id);
    }

}
